package com.yzc.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.db.SpringDB;
import com.yzc.user.UserBean;
import com.yzc.user.UserBeanMapper;
import com.yzc.user.UserBeanServiceImpl;
import com.yzc.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class loginController {

    @Autowired
    private UserBeanServiceImpl userService;
    @Autowired
    private UserBeanMapper userBeanMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login.do")
    public Map<String, Object> login(String name, String pass) {
        Map<String, Object> map = new HashMap<>();

        // 查询用户
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("pass", pass);
        List<UserBean> list = userBeanMapper.selectList(queryWrapper);

        if (!list.isEmpty()) {
            UserBean userBean = list.get(0);
            String token = jwtUtil.generateToken(userBean.getName());

            // 构建响应
            map.put("token", token);
            map.put("username", userBean.getName());
            map.put("userid", userBean.getId());
            map.put("result", "success");
        } else {
            map.put("result", "error");
        }

        return map;
    }
}
